package com.zkingsoft.services.impl;

import com.zkingsoft.services.RolePwoerService;
import com.zkingsoft.services.RoleService;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Role;
import com.zkingsoft.model.RolePwoer;
import com.zkingsoft.dao.RoleDao;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

import javax.annotation.Resource;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-04-22 08:43
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Resource
	private RolePwoerService pwoerService;

	@Transactional
	@Override
	public int add(Role role) {
		int i = roleDao.insert(role);
		if (role.getPwoers() != null) {
			for (RolePwoer rp : role.getPwoers()) {
				if (rp != null) {
					rp.setRoleId(role.getId());
					pwoerService.add(rp);
				}
			}
		}
		return i;
	}
	@Transactional
	@Override
	public int modify(Role role) {
		//删除旧权限
		pwoerService.removeByRoleIds(Arrays.asList(role.getId()));
		//添加新权限
		if (role.getPwoers() != null) {
			for (RolePwoer rp : role.getPwoers()) {
				if (rp != null) {
					rp.setRoleId(role.getId());
					pwoerService.add(rp);
				}
			}
		}
		return roleDao.update(role);
	}

	@Override
	public int remove(List<Long> list) {
		int i = roleDao.deleteByIds(list);
		//级联删除角色权限信息
		pwoerService.removeByRoleIds(list);
		return i ; 
		

	}
	@Transactional
	@Override
	public int removeById(Long id) {
		int i=roleDao.deleteById(id);
		//级联删除角色权限信息
		pwoerService.removeByRoleIds(Arrays.asList(id));
		return i;

	}

	@Override
	public List<Role> findInPage(Role role, PaginationVO pageVo) {

		return roleDao.selectInPage(role, pageVo);

	}

	@Override
	public List<Role> findByModel(Role role) {

		return roleDao.selectByModel(role);

	}

	@Override
	public int findTotal(Role role) {

		return roleDao.selectTotalRecord(role);

	}

	@Override
	public Role findById(Long id) {

		return roleDao.selectById(id);

	}

}